package com.sunlg.smarthome.ui.environment;

import android.support.v7.app.ActionBar;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.TextView;

import com.sunlg.smarthome.R;
import com.sunlg.smarthome.base.BaseActivity;
import com.sunlg.smarthome.contract.EnvirDeviceContract;
import com.sunlg.smarthome.contract.presenter.EnvirDevicePresenter;
import com.sunlg.smarthome.data.entity.EnvirDevice;
import com.sunlg.smarthome.ui.wedget.RecyclerViewDivider;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

public class EnvirDeviceListActivity extends BaseActivity implements EnvirDeviceContract.View{

    @BindView(R.id.rv_envir_device_list)
    RecyclerView rvEnvirDevice;
    @BindView(R.id.tv_none_device)
    TextView tvNoneDevice;

    private EnvriDeviceAdapter deviceAdapter;
    private LinearLayoutManager layoutManager;
    private List<EnvirDevice> envirDeviceList;
    private EnvirDeviceContract.Presenter presenter;

    @Override
    protected int getLayoutResourceId() {
        return R.layout.activity_envir_device_list;
    }

    @Override
    protected void setupToolBar(Toolbar toolbar) {
        super.setupToolBar(toolbar);
        ActionBar actionBar = getSupportActionBar();
        actionBar.setTitle("监测设备");
        actionBar.setDisplayHomeAsUpEnabled(true);

        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                doFinish();
            }
        });
    }

    @Override
    protected void init() {
        layoutManager = new LinearLayoutManager(this);
        envirDeviceList = new ArrayList<>();
        deviceAdapter = new EnvriDeviceAdapter(this, envirDeviceList);
        presenter = new EnvirDevicePresenter();
        presenter.attachView(this);
    }

    @Override
    protected void initView() {
        rvEnvirDevice.setLayoutManager(layoutManager);
        rvEnvirDevice.addItemDecoration(new RecyclerViewDivider(this, LinearLayoutManager.VERTICAL));
        rvEnvirDevice.setAdapter(deviceAdapter);
    }

    @Override
    protected void onStart() {
        super.onStart();
        presenter.loadAllEnvirDevice();
    }

    @Override
    public void showNoneDeviceText() {
        if(tvNoneDevice.getVisibility() != View.VISIBLE){
            tvNoneDevice.setVisibility(View.VISIBLE);
        }
    }

    @Override
    public void hideNoneDeviceText() {
        if(tvNoneDevice.getVisibility() == View.VISIBLE){
            tvNoneDevice.setVisibility(View.INVISIBLE);
        }
    }

    @Override
    public void showEnvirDeviceList(List<EnvirDevice> envirDeviceList) {
        this.envirDeviceList.clear();
        this.envirDeviceList.addAll(envirDeviceList);
        this.deviceAdapter.notifyDataSetChanged();
    }

    @Override
    public void showError(String msg) {
        toast(msg);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_add, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if(item.getItemId() == R.id.action_add){
            overlay(AddEnvirDeviceActivity.class);
        }
        return super.onOptionsItemSelected(item);
    }
}
